/**
 * @Title: CustomerPayCardController.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月20日
 */
package com.fortune.rainshine.system.controller;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.system.service.CustomerPayCardService;
import com.fortune.rainshine.system.service.CustomerService;
import com.fortune.rainshine.system.service.RedisService;
import com.fortune.rainshine.system.service.SmsSendFeign;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.RedisConstantEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.enums.SmsTypeEnum;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.model.SmsParamsModel;
import com.fortune.rainshinecommon.model.system.CustomerCardModel;
import com.fortune.rainshinecommon.model.system.CustomerPayCardModel;
import com.fortune.rainshinecommon.util.RandomNumUtil;
import com.google.gson.Gson;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author 张鑫
 * @ClassName: CustomerPayCardController
 * @Description: TODO(顾客服务)
 * @phone 17561783855
 * @date 2019年5月20日
 */
@Controller
@RequestMapping("/system/payCard")
public class CustomerPayCardController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(CustomerPayCardController.class);

	@Autowired
	private CustomerPayCardService service;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private RedisService redisFeign;
	@Autowired
	private SmsSendFeign smsSendFeign;
	@Value("${smsSend.number}")
	private Integer number;

	@RequiresPermissions("payCard:view")
	@RequestMapping()
	public String view(Model model) {
		// 获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		// 获取登录用户的按钮展示情况
		model.addAttribute("add", authorities.contains("payCard:add"));
		model.addAttribute("record", authorities.contains("payCard:record"));
		return "payCard/payCardList.html";
	}

	@RequestMapping("/editForm")
	public String edit(Model model) {
		return "payCard/payCard_form.html";
	}

	@RequiresPermissions("payCard:record")
	@RequestMapping("/record")
	public String record(String id, Model model) {
		List<CustomerPayCardModel> records = service.findDetailRecord(id);
		model.addAttribute("records", new Gson().toJson(records));
		return "payCard/payCardDetail.html";
	}

	/**
	 * @Title: add
	 * @Description: 会员服务
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月15日
	 */
	@RequiresPermissions("payCard:add")
	@ResponseBody
	@RequestMapping("/add")
	public MsgModel add(@RequestBody CustomerPayCardModel model) {
		try {
			// 判断用户是否存在
			CustomerModel customer = customerService.getCustomerById(model.getCustomerId());
			if (customer != null) {
				// redis key
				String key = RedisConstantEnum.PAYCARD.getMessage() + customer.getCustomerPhone();
				// 判断是否存在于redis中
				Boolean exists = redisFeign.exists(key);
				if (exists) {
					// 获取redis中的keyc
					String sms = (String) redisFeign.get(key).getData();
					if (sms.equals(model.getVerificationCode())) {
						model.setUser(this.getLoginUser());
						MsgModel msg = service.add(model);
						redisFeign.delete(key);
						return msg;
					} else {
						return new MsgModel("验证码不正确", ResultCode.FAILURE);
					}
				} else {
					return new MsgModel("验证码不存在或者已过期", ResultCode.FAILURE);
				}
			} else {
				return new MsgModel("用户不存在", ResultCode.FAILURE);
			}
		} catch (Exception e) {
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
	}

	/**
	 * @Title: list
	 * @Description: 获取列表
	 * @author zhangxin
	 * @date 2019年3月26日
	 */
	@RequiresPermissions("payCard:view")
	@ResponseBody
	@GetMapping("/list")
	public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId) {
		try {
			if (page == null) {
				page = 0;
				limit = 0;
			}
			if (StringUtil.isBlank(searchValue)) {
				searchKey = null;
			}

			ParmsModel parm = new ParmsModel(page, limit, searchKey, searchValue, shopId);
			return service.list(parm);
		} catch (Exception e) {
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	/**
	 * @Title: sms
	 * @Description: 会员服务时获取验证码
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月21日
	 */
	@RequiresPermissions("payCard:sms")
	@ResponseBody
	@RequestMapping("/sms")
	public MsgModel sms(@RequestBody CustomerCardModel customerCard) {
		try {
			CustomerModel customer = customerService.getCustomerById(customerCard.getCustomerId());
			String phone = customer.getCustomerPhone();
			Boolean exists = redisFeign.exists(RedisConstantEnum.PAYCARD.getMessage() + phone);
			Boolean bo = false;
			if (exists) {
				redisFeign.delete(RedisConstantEnum.PAYCARD.getMessage() + phone);
			}
			Gson gson = new Gson();
			String code = RandomNumUtil.getRandNum(number);
			SmsParamsModel smsParams = new SmsParamsModel();
			smsParams.setCode(code);
			smsParams.setSmsType(SmsTypeEnum.serviceValid.getKey());
			String jsonParams = gson.toJson(smsParams);

			bo = smsSendFeign.sendSms(phone, jsonParams);
			if (bo) {
				redisFeign.set(RedisConstantEnum.PAYCARD.getMessage() + phone, code, 60000);
			}
			return new MsgModel(ResultCode.SUCCESS);
		} catch (Exception e) {
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
	}

}
